import java.util.ArrayList;
import java.util.List;

/**
 * 实现一种转换方式
 */
public class TransForm implements ITransform {
    public char[] wordsToRoman(String[] wordArray) {
        List<Character> list = new ArrayList<Character>();
        for (int i= 0; i <wordArray.length ;i++){
            Character roman = RomanNumStatic.scoreMap.get(wordArray[i]);
            if (roman== null){
                break;
            }
            list.add(roman);
        }
        Character[] romanArray = new Character[list.size()];
        char[] returnCharArray = new char[list.size()];
        list.toArray(romanArray);
        for (int i = 0 ; i <list.size() ; i ++){
            returnCharArray[i] = romanArray[i].charValue();
        }
        return  returnCharArray;
    }

    public int[] romanArrayToAribicArray(char[] roman) {
        int[] arabicArray = new int[roman.length];
        for (int i = 0; i < roman.length; i++) {
            int a = 0;
            switch (roman[i]) {
                case 'I':
                    a = 1;
                    break;
                case 'V':
                    a = 5;
                    break;
                case 'X':
                    a = 10;
                    break;
                case 'L':
                    a = 50;
                    break;
                case 'C':
                    a = 100;
                    break;
                case 'D':
                    a = 500;
                    break;
                case 'M':
                    a = 1000;
                    break;

                default:
                    throw new RuntimeException("RomanNum is out of range");
            }
            arabicArray[i] = a;
        }
        return arabicArray;
    }

    public int toArabic(char[] roman) {
        //得到整型数组
        int[] arabicArray = romanArrayToAribicArray(roman);
        //根据整型数组转换得到阿拉伯数字
        int sum = 0;
        for (int i = 0; i < arabicArray.length; i++) {
            if (i + 1 == arabicArray.length) {
                //已到最后一位
                sum += arabicArray[i];
                break;
            }
            if (arabicArray[i + 1] > arabicArray[i]) {
                //后一位大于前一位
                sum += (arabicArray[i + 1] - arabicArray[i]);
                i++;
            } else {
                //前一位大于后一位
                sum += arabicArray[i];
            }
        }
        return sum;

    }
}
